package com.intgrt.manager.service.base.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.intgrt.manager.jdbc.BaseJdbc;
import com.intgrt.manager.util.ProjectTools;

public class BaseServiceImpl {

	@Autowired
	private BaseJdbc baseDao;
	
	protected int execute(String sql){
		try {
			baseDao.execSql(sql);
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
	}
	
	protected int[] batch(List<String> sqllst){
		return baseDao.execBatch(sqllst);
	}
	
	protected long getCount(String cdtSql){
		String numSql = "select count(1) from "+cdtSql;
		List<Map<String,Object>> reslst = baseDao.getMapList(numSql);
		String cntNum = String.valueOf(reslst.get(0).get("count(1)"));
		return Long.parseLong(cntNum);
	}
	
	protected List<String> getStrField(String sql, String field){
		List<String> strlst = new ArrayList<String>();
		List<Map<String,Object>> reslst = baseDao.getMapList(sql);
		if(reslst.size()>0){
			for(Map<String,Object> resmap:reslst){
				strlst.add(String.valueOf(resmap.get(field)));
			}
		}
		return strlst;
	}
	
	protected List<Map<String,Object>> getMapList(String sql){
		return baseDao.getMapList(sql);
	}
	
	protected JSONObject getPageDate(String content, String cdtSql, int pagenum, int pagesize){
		List<Map<String,Object>> reslst = new ArrayList<Map<String,Object>>();
		JSONObject resjson = new JSONObject();
		long total = this.getCount(cdtSql);
		if(total>0){
			int start = (pagenum - 1)*pagesize;
			String sql = "select "+content+" from "+cdtSql+" limit "+start+","+pagesize;
			reslst = this.getMapList(sql);
			
		}
		resjson.put("page", total/pagesize);
		resjson.put("total", total);
		resjson.put("size", pagenum);
		resjson.put("rows", reslst);
		return resjson;
	}
	
	protected String getOrgPageDate (String content, String cdtSql, int pagenum, int pagesize) {
		JSONObject jo = getPageDate(content, cdtSql, pagenum, pagesize);
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> reslst = (List<Map<String, Object>>) jo.get("rows");
		if(reslst.size()>0){
			StringBuilder orgbuf = new StringBuilder();
			for(Map<String,Object> resmap:reslst){
				String orgid = String.valueOf(resmap.get("orgid"));
				if(orgbuf.indexOf(","+orgid+",")<0){
					orgbuf.append(","+orgid);
				}
			}
			Map<String,Object> orgmap = getOrgMap(orgbuf.substring(1));
			for(Map<String,Object> resmap:reslst){
				String orgid = String.valueOf(resmap.get("orgid"));
				resmap.put("orgname", orgmap.get(orgid));
			}
			jo.put("rows", reslst);
		}
		return jo.toJSONString();
	}
	
	protected String getSysOrgids(String orgid) {
		String sql = "select id from sys_organize where parentids like '%,"+orgid+",%' or id="+orgid;
		List<Map<String,Object>> reslst = baseDao.getMapList(sql);
		StringBuffer orgbuf = new StringBuffer();
		for(Map<String,Object> map : reslst){
			orgbuf.append(","+map.get("id"));
		}
		return orgbuf.substring(1);
	}

	protected String getSubOrgids(String orgid) {
		String sql = "select id from sys_organize where parentid = "+orgid+" or id="+orgid;
		List<Map<String,Object>> reslst = baseDao.getMapList(sql);
		StringBuffer orgbuf = new StringBuffer();
		for(Map<String,Object> map : reslst){
			orgbuf.append(","+map.get("id"));
		}
		return orgbuf.substring(1);
	}
	
	protected Map<String,Object> getOrgMap(String orgids) {
		Map<String,Object> orgmap = new HashMap<String,Object>();
		List<Map<String,Object>> orglst = this.getMapList("select id, orgname from sys_organize where id in ("+orgids+")");
		for(Map<String,Object> resmap:orglst){
			orgmap.put(String.valueOf(resmap.get("id")), resmap.get("orgname"));
		}
		return orgmap;
	}
	
	public JSONArray getMenuBtns(String id, String menuid, String btnids){
		JSONArray btnArray = new JSONArray();
		String sql = "select btnname,btnicon,btnfunc from sys_menu_btn where btnshow = 0 ";
		if("1".equals(id)){
			sql += "and menuid = "+menuid;
		}else{
			if(ProjectTools.isEmpty(btnids)){
				return btnArray;
			}
			sql += "and id in ("+btnids+")";
		}
		sql	+= " order by btnorder";
		List<Map<String,Object>> reslst = this.getMapList(sql);
		if(reslst.size()>0){
			for(Map<String,Object> resmap:reslst){
				JSONObject resjson = new JSONObject();
				resjson.put("btnname", String.valueOf(resmap.get("btnname")));
				resjson.put("btnicon", String.valueOf(resmap.get("btnicon")));
				resjson.put("btnfunc", String.valueOf(resmap.get("btnfunc")));
				btnArray.add(resjson);
			}
		}
		return btnArray;
	}
	
}
